﻿<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>身份证的验证</title>
    <script type="text/javascript" language="JavaScript">
        function isCardNo(num)
        {
            if (isNaN(num)) {
                alert("输入的不全是数字！"); 
                return false;
            }
            let len = num.length, re;
            if (len === 15)       //15位身份证的判断
                re = new RegExp(/^(\d{6})()?(\d{2})(\d{2})(\d{2})(\d{3})$/);
            else if (len === 18)  //18位身份证的判断
                re = new RegExp(/^(\d{6})()?(\d{4})(\d{2})(\d{2})(\d{3})(\d)$/);
            else {
                alert("输入的数字位数不对！"); 
                return false;
            }
            let a = num.match(re);//判断是否有符合条件的表达式
            if (a !== null)
            {
                if (len === 15)
                {                  //对15位身份证中日期的判断
                    let D = new Date("19"+a[3]+"/"+a[4]+"/"+a[5]);
                    let B = D.getYear() ===a[3] && 
                        (D.getMonth()+1) === a[4]
                        && D.getDate() === a[5];
                }
                else
                {                  //对18位身份证中日期的判断
                    let D = new Date(a[3]+"/"+a[4]+"/"+a[5]);
                    let B = D.getFullYear() === a[3]
                        &&(D.getMonth()+1) === a[4]
                        &&D.getDate() === a[5];
                }
                if (!B) {alert("输入的身份证号 "+ a[0] +" 里出生日期不对！"); return false;}
            }
            return true;
        }
        
    </script>
</head>
<body>
<input type=text name="txt1" value="1110120780423003">
<input type=button value="判断身份证号" onClick="javascript:alert(isCardNo(txt1.value))">
</body>
</html>